﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace OxyPlotDemo
{
    public class Timer : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private int[] TimeNow = new int[3] { 0, 0, 0 };
        private string[] TimeNowToSubString = new string[3];
        private string timeNowToString;
        public string TimeNowToString
        {
            get { return timeNowToString; }
            set
            {
                timeNowToString = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("TimeNowToString"));
                }
            }
        }
        public void TimerStart(int n)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(n);
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            TimeNow[0]++;
            for (int i = 0; i < 2; i++)
            {
                if (TimeNow[i] == 60)
                {
                    TimeNow[i] = 0;
                    TimeNow[i + 1]++;
                }
            }
            for (int i = 0; i < 3; i++)
            {
                if (TimeNow[i] < 10)
                    TimeNowToSubString[i] = "0" + TimeNow[i].ToString();
                else
                    TimeNowToSubString[i] = TimeNow[i].ToString();
            }
            //((MainWindow)System.Windows.Application.Current.MainWindow).TimerTextBlock.Text = TimeNowToString[2] + ":" + TimeNowToString[1] + ":" + TimeNowToString[0];
            TimeNowToString = TimeNowToSubString[2] + ":" + TimeNowToSubString[1] + ":" + TimeNowToSubString[0];
        }
    }
}
